﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using MyWeb.ServiceBus.Common;
using MyWeb.ServiceBus.Model;
using MyWeb.ServiceBus.Model.Models;
using MyWeb.ServiceBus.Services;
using MyWeb.ServiceBus.Services.Db;

namespace MyWeb.ServiceBus.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class AppSystemController : ControllerBase
    {
        private IAppSystemService _service;
        public AppSystemController(IAppSystemService service)
        {
            _service = service;
        }

        [HttpGet]
        public async Task<IActionResult> Get()
        {
            var org = await _service.QueryAsync(o => 1 == 1);
            return Ok(org);
        }
        [HttpGet]
        public ApiResult GetPage(int page, int pageSize)
        {
            long count = 0;
            // 执行分页查询
            List<AppSystem> apps = _service.QueryPage(out count,page, pageSize);
            var pageData = new Model.PageModel(page, (int)count, pageSize, apps);

            return ApiResult.SUCCESS(res: pageData);
        }

        [HttpPost]
        public async Task<ApiResult> Add(AppSystem app)
        {
            app.CreateDate = DateTime.Now;
            app.Id = Snowflake.NextId();
            var com = await _service.AddAsync(app);
            return com >= 1 ? ApiResult.SUCCESS(): ApiResult.FAILED();
        }
        [HttpPut]
        public async Task<ApiResult> Edit(AppSystem app)
        {
            app.UpdateDate = DateTime.Now;
            var com = await _service.EditAsync(app);
            return com >= 1 ? ApiResult.SUCCESS() : ApiResult.FAILED();
        }
        [HttpDelete]
        public async Task<ApiResult> Remove(long id)
        {
            var app = await _service.QueryAsync(a => a.Id == id);
            var com = await _service.DeleteAsync(app.First());
            return com >= 1 ? ApiResult.SUCCESS() : ApiResult.FAILED();
        }
    }
}
